package com.ggh.jinjilive.ui.home.follow.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemLiveListBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.bumptech.glide.Glide;

import java.util.List;

public class FollowAdapter extends RecyclerView.Adapter<FollowAdapter.ItemViewHolder> {

    private List<LiveList> liveListList;


    public void setAdd(List<LiveList> liveListList) {
        this.liveListList = liveListList;
        notifyDataSetChanged();
    }

    public void Add(List<LiveList> liveListList) {
        if(null != this.liveListList){
            this.liveListList.addAll(liveListList);
        }else {
            this.liveListList = liveListList;
        }
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemLiveListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_live_list, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        LiveList liveList = liveListList.get(position);
        if(null != liveList){
            Glide.with(holder.binding.RoundedImageView.getContext()).load(liveList.getCover()).into(holder.binding.RoundedImageView);
            holder.binding.tvNumber.setText(liveList.getNumber() +"");
            if (liveList.getType() == 0){
                holder.binding.roomType.setText("普通");
            }else if (liveList.getType() == 1){
                holder.binding.roomType.setText("密码");
            }else if (liveList.getType() == 2){
                holder.binding.roomType.setText("付费");
            }else if (liveList.getType() == 3) {
                holder.binding.roomType.setText("计时");
            }
            holder.binding.tvName.setText(liveList.getTitle());
            holder.binding.tvName.setSelected(true);
            holder.binding.llLive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,liveList);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == liveListList ? 0 : liveListList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemLiveListBinding binding;

        public ItemViewHolder(@NonNull ItemLiveListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
